package edu.whut.mall.admin.controller;

import edu.whut.mall.admin.model.Admin;
import edu.whut.mall.admin.model.AdminRole;
import edu.whut.mall.admin.dto.AdminDto;
import edu.whut.mall.admin.model.Role;
import edu.whut.mall.admin.service.IAdminService;
import edu.whut.mall.admin.vo.AdminVo;
import edu.whut.mall.common.util.IpUtil;
import edu.whut.mall.common.api.ResponseMap;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * Author: smq
 * Unit: D9lab
 * Date: 2021-06-07 16:15
 */
@Api(tags = {"平台管理员管理"}, description = "", produces = MediaType.APPLICATION_JSON_VALUE)
@RestController
@RequiredArgsConstructor
@RequestMapping("/admin")
public class AdminController {


    private final IAdminService adminService;

    @ApiOperation(value = "最高管理员添加管理员")
    @RequestMapping(value = "/register", method = RequestMethod.POST)
    public Map<String,Object> register(HttpServletRequest request, @RequestBody AdminDto adminDto) throws Exception {
        ResponseMap map = ResponseMap.getInstance();
        Admin result = adminService.insertOneUser(IpUtil.getIpAddr(request), adminDto);
        if (result == null) {
            return map.putFailure("注册成功", 0);
        }
        return map.putValue(result, "注册失败");
    }

    @ApiOperation(value = "给指定管理员分配角色")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "adminId", value = "", paramType = "form", dataType = "int", required = true),
            @ApiImplicitParam(name = "roleIdString", value = "", paramType = "form", dataType = "string", required = true),
    })
    @RequestMapping(value = "/assignRoleUser", method = RequestMethod.POST)
    public Map<String,Object> assignRoleUser(HttpServletRequest request, Integer adminId, String roleIdString) throws Exception {
        ResponseMap map = ResponseMap.getInstance();
        List<AdminRole> result = adminService.assignRoleUser(adminId, roleIdString);
        if (result == null) {
            return map.putFailure("分配失败", 0);
        }
        return map.putValue(result, "分配成功");
    }

    @ApiOperation("获取指定用户的角色")
    @RequestMapping(value = "/role/{adminId}", method = RequestMethod.GET)
    @ResponseBody
    public Map<String,Object> getRoleList(@PathVariable Integer adminId) {
        ResponseMap map = ResponseMap.getInstance();
        List<Role> roleList = adminService.getRoleList(adminId);
        return map.putValue(roleList,"获取成功");
    }

    @ApiOperation("获取指定管理员信息")
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public Map<String,Object> getItem(@PathVariable Integer id) {
        ResponseMap map = ResponseMap.getInstance();
        AdminVo admin = adminService.getItem(id);
        return map.putValue(admin, "获取成功");
    }

    @ApiOperation("修改指定管理员信息")
    @RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> update(@PathVariable Integer id, @RequestBody AdminDto adminDto) {
        ResponseMap map = ResponseMap.getInstance();
        int count = adminService.updateAdmin(id, adminDto);
        if (count > 0) {
            return map.putValue(count, "更新成功");
        }
        return map.putFailure("更新失败", 0);
    }

    @ApiOperation("删除指定用户信息")
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> delete(@PathVariable Integer id) {
        ResponseMap map = ResponseMap.getInstance();
        int count = adminService.deleteAdminById(id);
        if (count > 0) {
            return map.putValue(count, "删除成功");
        }
        return map.putFailure("删除失败", 0);
    }

    @ApiOperation("修改帐号状态")
    @RequestMapping(value = "/updateStatus/{id}", method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> updateStatus(@PathVariable Integer id, @RequestParam(value = "status") Integer status) {
        ResponseMap map = ResponseMap.getInstance();
        int count = adminService.updateStatus(id, status);
        if (count > 0) {
            return map.putValue(count,"修改成功");
        }
        return map.putFailure("修改失败", 0);
    }
}


